package com.example.testapplication.ui.guide;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;

import com.example.testapplication.BR;
import com.example.testapplication.R;
import com.example.testapplication.binding.viewPager2.ViewAdapter;
import com.example.testapplication.databinding.ActivityGuideBinding;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import me.goldze.mvvmhabit.base.BaseActivity;

public class GuideActivity extends BaseActivity<ActivityGuideBinding,GuideViewModel> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_guide;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public GuideViewModel initViewModel() {
        return new GuideViewModel(getApplication());
    }

    @NonNull
    @Override
    protected ImmersionBar createStatusBarConfig() {
        return ImmersionBar.with(this)
                // 默认状态栏字体颜色为黑色
                .statusBarDarkFont(false)
                // 隐藏导航栏
                .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR);
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.pClickEvent.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                if(o instanceof ViewAdapter.ScrollDataWrapper){
                    if (binding.vpGuidePager.getCurrentItem() != viewModel.observableList.size() - 1 || ((ViewAdapter.ScrollDataWrapper) o).positionOffsetPixels <= 0) {
                        return;
                    }

                    binding.indicator.setVisibility(View.VISIBLE);
                    binding.btnGuideComplete.setVisibility(View.INVISIBLE);
                    binding.btnGuideComplete.clearAnimation();
                }else if(o instanceof Integer){
                    if ((int)o != ViewPager2.SCROLL_STATE_IDLE) {
                        return;
                    }
                    viewModel.indicator.set(binding.vpGuidePager.getCurrentItem()+1+"/3");
                    boolean lastItem = binding.vpGuidePager.getCurrentItem() == viewModel.observableList.size() - 1;
                    binding.indicator.setVisibility(lastItem ? View.INVISIBLE : View.VISIBLE);
                    binding.btnGuideComplete.setVisibility(lastItem ? View.VISIBLE : View.INVISIBLE);
                    if (lastItem) {
                        // 按钮呼吸动效
                        ScaleAnimation animation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f,
                                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        animation.setDuration(350);
                        animation.setRepeatMode(Animation.REVERSE);
                        animation.setRepeatCount(Animation.INFINITE);
                        binding.btnGuideComplete.startAnimation(animation);
                    }
                }
            }
        });
    }
}